package com.qst.crop.controller;

import com.qst.crop.common.Result;
import com.qst.crop.common.StatusCode;
import com.qst.crop.entity.*;
import com.qst.crop.security.entity.JwtUser;
import com.qst.crop.service.FinanceService;
import com.qst.crop.service.FinancingIntentionService;
import com.qst.crop.service.UserService;
import com.sun.prism.impl.BaseContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/finance")
@Api("融资相关接口")
public class FinanceController {

    @Autowired
    private FinanceService financeService;

    @Autowired
    private FinancingIntentionService financingIntentionService;

    @Autowired
    private UserService userService;

    /**
     * @Description : 个人融资
     *  * @param finance
     * @Return : com.qst.crop.common.Result
     */
    @PostMapping("/add")
    @ApiOperation("个人融资")
    public Result addPersonal(@RequestBody Finance finance){
        log.info("个人融资");
        finance.setCreateTime(LocalDateTime.now());
        finance.setUpdateTime(LocalDateTime.now());
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        log.info("name=" + username);
        finance.setOwnName(username);
        finance.setStatus(0);
        financeService.addPersonal(finance);
        return new Result<>(true,StatusCode.OK,"个人融资添加成功");
    }

    /**
     * @Description : 组合融资
     *  * @param finance
     * @Return : com.qst.crop.common.Result
     */
    @PostMapping("/addFinanceMulti")
    @ApiOperation("组合融资")
    public Result addMulti(@RequestBody Finance finance){
        log.info("组合融资");
        finance.setCreateTime(LocalDateTime.now());
        finance.setUpdateTime(LocalDateTime.now());
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        log.info("name=" + username);
        finance.setOwnName(username);
        finance.setStatus(0);
        financeService.addMulti(finance);
        return new Result<>(true,StatusCode.OK,"组合融资添加成功");
    }

    /**
     * @Description : 查询银行
     *  * @param
     * @Return : com.qst.crop.common.Result<java.util.List<com.qst.crop.entity.Bank>>
     */
    @GetMapping("/selectbank")
    @ApiOperation("查询银行")
    public Result<List<Bank>> getBank(){
        log.info("查询银行");
        List<Bank> list = financeService.selectBank();
        return new Result<>(true, StatusCode.OK,"查询成功",list);
    }

    /**
     * @Description : 推荐融资人
     *  * @param
     * @Return : com.qst.crop.common.Result<java.util.List<com.qst.crop.entity.RecommendVO>>
     */
//    @GetMapping("/selectRecommendByName")
//    @ApiOperation("推荐融资人")
//    public Result<List<RecommendVO>> selectRecommendByName(){
//
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        JwtUser principal = (JwtUser) authentication.getPrincipal();
//        String username = principal.getUsername();
//        List<RecommendVO> recommendVOlist = financeService.selectRecommandByName(username);
//        return new Result<>(true,StatusCode.OK,"推荐成功",recommendVOlist);
//
//    }

    /**
     * @Description : 查询融资意向
     *  * @param
     * @Return : com.qst.crop.common.Result<java.util.List<com.qst.crop.entity.FinancingIntention>>
     */
    @GetMapping("/selectIntentionByName")
    @ApiOperation("查询融资意向")
    public Result<List<FinancingIntention>> selectIntentionByName(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        List<FinancingIntention> financingIntentionList = financingIntentionService.selectIntentionByName(username);
        return new Result<>(true,StatusCode.OK,"推荐成功",financingIntentionList);
    }

    /**
     * @Description : 删除融资意向
     *  * @param
     * @Return : com.qst.crop.common.Result<>
     */
    @DeleteMapping("/deleteIntentionByName")
    @ApiOperation("删除融资意向")
    public Result deleteIntentionByName(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        financingIntentionService.deleteIntentionByName(username);
        return new Result<>(true,StatusCode.OK,"删除成功");
    }

    /**
     * @Description : 添加融资意向
     *  * @param financingIntention
     * @Return : com.qst.crop.common.Result
     */
    @PostMapping("/insertIntentionByName")
    @ApiOperation("添加融资意向")
    public Result insertIntentionByName(@RequestBody FinancingIntention financingIntention){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        financingIntention.setUserName(username);
        financingIntentionService.insertIntensionByName(financingIntention);
        return new Result<>(true,StatusCode.OK,"添加成功");
    }

    /**
     * @Description : 更新融资意向
     *  * @param financingIntention
     * @Return : com.qst.crop.common.Result
     */
    @PutMapping("/updateIntentionByName")
    @ApiOperation("更新融资意向")
    public Result updateIntentionByName(@RequestBody FinancingIntention financingIntention){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        financingIntention.setUserName(username);
        financingIntentionService.updateIntensionByName(financingIntention);
        return new Result<>(true,StatusCode.OK,"更新成功");
    }

    /**
     * @Description : 查询个人融资
     *  * @param
     * @Return : com.qst.crop.common.Result<java.util.List<com.qst.crop.entity.Finance>>
     */
    @GetMapping("/selectByName/")
    @ApiOperation("查询个人融资")
    public Result<List<Finance>> selectByName(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        return new Result<>(true,StatusCode.OK,"查询成功",financeService.selectByName(username));
    }

    /**
     * @Description : 通过Id查询个人融资
     *  * @param id
     * @Return : com.qst.crop.common.Result<com.qst.crop.entity.Finance>
     */
    @GetMapping("/selectById/{id}")
    @ApiOperation("通过Id查询个人融资")
    public Result<Finance> selectById(@PathVariable Integer id){

        return new Result<>(true,StatusCode.OK,"查询成功",financeService.selectById(id));
    }

    /**
     * @Description : 删除融资
     *  * @param id
     * @Return : com.qst.crop.common.Result
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除融资")
    public Result delete(@PathVariable Integer id){

        financeService.deleteById(id);
        return new Result<>(true,StatusCode.OK,"删除成功");
    }

    /**
     * @Description : 更新融资
     *  * @param finance
     * @Return : com.qst.crop.common.Result
     */
    @PutMapping("/update")
    @ApiOperation("更新融资")
    public Result updateByName(@RequestBody Finance finance){

        financeService.updateByName(finance);
        return new Result<>(true,StatusCode.OK,"更新成功");
    }

    @GetMapping("/selectFinaceUser/{id}")
    public Result<FinanceDetail> selectFinaceUser(@PathVariable("id") Integer bankId) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        FinanceDetail fd = financeService.selectFinaceUser(username,bankId);
        log.info("{}",fd);
        if(fd == null){
            fd = financeService.showBank(bankId);
            fd.setRealName(userService.getRealName(username));
        }
        log.info("{}",fd);
        log.info("{}",fd);


        return new Result<>(true,StatusCode.OK,"success",fd);
    }

    @ApiOperation(value = "查询推荐融资人")
    @GetMapping("/selectRecommendByName")
    public Result selectRecommendByName(){
        //获取登陆的用户名
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        List<RecommendVO> list = financeService.selectRecommend(name);

        return new Result(true, StatusCode.OK, "查询成功",list);
    }

    @GetMapping("/getFinanceBankId")
    public Result<List<Integer>> getAllBid(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();
        return new Result<>(true,StatusCode.OK,"success",financeService.getAllBid(username));
    }

}
